package top.itmeans.yes.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.itmeans.yes.entity.Resource;
import top.itmeans.yes.mapper.ResourceMapper;
import top.itmeans.yes.service.ResourceService;

import java.time.LocalDateTime;

/**
 * <p>
 * 后台资源表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {
    @javax.annotation.Resource
    private ResourceMapper resourceMapper;

    @Override
    public int create(Resource resource) {
        resource.setCreateTime(LocalDateTime.now());
        return resourceMapper.insert(resource);
    }

    @Override
    public int update(Long id, Resource resource) {
        resource.setId(id);
        return resourceMapper.updateById(resource);
    }

    @Override
    public Resource getItem(Long id) {
        return resourceMapper.selectById(id);
    }

    @Override
    public int delete(Long id) {
        return resourceMapper.deleteById(id);
    }

    @Override
    public Page<Resource> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        Page<Resource> resourcePage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Resource> queryWrapper = new LambdaQueryWrapper<>();
        if(categoryId!=null){
            queryWrapper.eq(Resource::getCategoryId,categoryId);
        }
        if(StrUtil.isNotEmpty(nameKeyword)){
            queryWrapper.like(Resource::getName,nameKeyword);
        }
        if(StrUtil.isNotEmpty(urlKeyword)){
            queryWrapper.like(Resource::getUrl,urlKeyword);
        }
        return resourceMapper.selectPage(resourcePage,queryWrapper);
    }
}
